package com.wfg.common;

import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.apache.juli.logging.Log;

import javax.management.ObjectName;
import java.beans.PropertyChangeListener;
import java.io.File;

/**
 * @author wufagang
 * @description
 * @date 2023年05月10日 23:30
 */
public class HttpServer {


    public void start1(String hostName, Integer port){
        // 创建tomcat服务器
        Tomcat tomcatServer = new Tomcat();
        // 指定端口号
        tomcatServer.setPort(port);
        // 是否设置自动部署
        tomcatServer.getHost().setAutoDeploy(false);
        // 创建上下文
        StandardContext standardContex = new StandardContext();
        standardContex.setPath("");
        // 监听上下文
        standardContex.addLifecycleListener(new Tomcat.FixContextListener());
        // tomcat容器添加standardContex
        tomcatServer.getHost().addChild(standardContex);

//        // 创建Servlet
//        tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAME, new IndexServlet());
//        tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAMET, new IndexServletC());
//        // servleturl映射
//        standardContex.addServletMappingDecoded("/index", SERVLET_NAME);
//        standardContex.addServletMappingDecoded("/indexc", SERVLET_NAMET);
        tomcatServer.addServlet(standardContex,"dispatcher", new Dispatcher());
        standardContex.addServletMappingDecoded("/*","dispatcher");
        try {
            tomcatServer.start();
            System.out.println("tomcat start success..");
            // 异步进行接收请求
            tomcatServer.getServer().await();
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }

    }

    public void start(String hostName, Integer port){
        Tomcat tomcat = new Tomcat();
        Server server = tomcat.getServer();
        Service service = server.findService("Tomcat");

        Connector connector = new Connector();
        connector.setPort(port);

        Engine engine = new StandardEngine();
        engine.setDefaultHost(hostName);

        Host host = new StandardHost();
        host.setName(hostName);

        String contextPath = "";
        Context context = new StandardContext();

        context.setPath(contextPath);
        context.addLifecycleListener(new Tomcat.FixContextListener());

        host.addChild(context);
        engine.addChild(host);

        service.setContainer(engine);
        service.addConnector(connector);

        tomcat.addServlet(contextPath,"dispatcher", new Dispatcher());
        context.addServletMappingDecoded("/*","dispatcher");
        try {
            tomcat.start();
            tomcat.getServer().await();
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
    }
}
